{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6af72627-f120-4af6-bf0e-04eca6968e65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"EDUa49\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.4.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"EDUa49\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"EDUa49\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23eddfc8-aeb7-4b84-a8ad-51a469df83b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.0.0.0-SNAPSHOT. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.4.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "20420e91-9dcc-4592-a7b7-414eb33f9621",
   "metadata": {},
   "outputs": [],
   "source": [
    "val rand = java.util.Random(42)\n",
    "\n",
    "val n = 6\n",
    "val x = (0 until n).toList()\n",
    "val y1 = List(n) { rand.nextDouble() }\n",
    "val y2 = List(n) { rand.nextDouble() }\n",
    "val v = List(n) { \"abc\".random() }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d02851-0110-4fa2-a319-1088dc02e890",
   "metadata": {},
   "source": [
    "#### Short form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6c623c67-baf5-4a27-8260-8c528354947f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"FbbMtQ\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":\"Red zone\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":\"Blue zone\",\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":\"Green zone\",\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":\"Orange zone\",\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"FbbMtQ\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot() +\n",
    "    geomPoint(data = mapOf(\"x\" to x, \"y\" to y1), color = \"red\", shape = 21, manualKey = \"Red zone\") { x = \"x\"; y = \"y\" } +\n",
    "    geomLine(data = mapOf(\"x\" to x, \"y\" to y2), color = \"blue\", linetype = 2, manualKey = \"Blue zone\")  { x = \"x\"; y = \"y\" } +\n",
    "    geomRect(xmin = 6, xmax = 8, ymin = 0.7, ymax = 0.8, fill = \"green\", alpha = 0.2, manualKey = \"Green zone\") +\n",
    "    geomLabel(label = \"Text\", x = 6, y = 0.2, fill = \"orange\", color = \"white\", size = 8, manualKey = \"Orange zone\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02578261-f729-44e9-abe8-84127edae402",
   "metadata": {},
   "source": [
    "#### Parameterized form\n",
    "\n",
    "```\n",
    "fun layerKey(\n",
    "    label: String,\n",
    "    group: String? = null,\n",
    "    index: Int? = null,\n",
    "    aes: Map<Aes<*>, Any>? = null\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d8912a06-a0d1-45c3-8f96-5e215376cf92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"12bCug\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Red zone\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"Blue zone\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Green zone\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"label\":\"Orange zone\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"12bCug\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import org.jetbrains.letsPlot.core.plot.base.Aes\n",
    "\n",
    "val p1 = letsPlot() +\n",
    "    geomPoint(data = mapOf(\"x\" to x, \"y\" to y1), color = \"red\", shape = 21, \n",
    "              manualKey = layerKey(\"Red zone\")) { x = \"x\"; y = \"y\" } +\n",
    "    geomLine(data = mapOf(\"x\" to x, \"y\" to y2), color = \"blue\", linetype = 2, \n",
    "             manualKey = layerKey(\"Blue zone\")) { x = \"x\"; y = \"y\" } +\n",
    "    geomRect(xmin = 6, xmax = 8, ymin = 0.7, ymax = 0.8, fill = \"green\", alpha = 0.2, \n",
    "             manualKey = layerKey(\"Green zone\", alpha = 1)) +\n",
    "    geomLabel(label = \"Text\", x = 6, y = 0.2, fill = \"orange\", color = \"white\", size = 8, \n",
    "              manualKey = layerKey(\"Orange zone\", color = \"black\"))\n",
    "\n",
    "p1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c4c7512-75d4-4020-b899-e854b6c7ab87",
   "metadata": {},
   "source": [
    "Reorder using index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0beb2fd6-ec23-4877-bb6e-9ac2ebb20449",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"gaKjrK\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"index\":1,\n",
       "\"label\":\"Red zone\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"index\":3,\n",
       "\"label\":\"Blue zone\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"index\":2,\n",
       "\"label\":\"Green zone\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"index\":0,\n",
       "\"label\":\"Orange zone\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"gaKjrK\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot() +\n",
    "    geomPoint(data = mapOf(\"x\" to x, \"y\" to y1), color = \"red\", shape = 21, \n",
    "              manualKey = layerKey(\"Red zone\", index = 1)) { x = \"x\"; y = \"y\" } +\n",
    "    geomLine(data = mapOf(\"x\" to x, \"y\" to y2), color = \"blue\", linetype = 2, \n",
    "             manualKey = layerKey(\"Blue zone\", index = 3)) { x = \"x\"; y = \"y\" } +\n",
    "    geomRect(xmin = 6, xmax = 8, ymin = 0.7, ymax = 0.8, fill = \"green\", alpha = 0.2, \n",
    "             manualKey = layerKey(\"Green zone\", index = 2, alpha = 1)) +\n",
    "    geomLabel(label = \"Text\", x = 6, y = 0.2, fill = \"orange\", color = \"white\", size = 8, \n",
    "              manualKey = layerKey(\"Orange zone\", index = 0, color = \"black\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9c9bd53-6a83-41c6-ae10-3ffa7bbf98c8",
   "metadata": {},
   "source": [
    "Use `group` to put items to different legend groups:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "09bf5597-3920-4d20-a16a-a9ceb60bf6c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"zhSBhS\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Red zone\",\n",
       "\"group\":\"Group1\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"Blue zone\",\n",
       "\"group\":\"Group1\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Green zone\",\n",
       "\"group\":\"Group2\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"label\":\"Orange zone\",\n",
       "\"group\":\"Group3\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"zhSBhS\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val p2 = letsPlot() +\n",
    "    geomPoint(data = mapOf(\"x\" to x, \"y\" to y1), color = \"red\", shape = 21, \n",
    "              manualKey = layerKey(\"Red zone\", \"Group1\")) { x = \"x\"; y = \"y\" } +\n",
    "    geomLine(data = mapOf(\"x\" to x, \"y\" to y2), color = \"blue\", linetype = 2, \n",
    "             manualKey = layerKey(\"Blue zone\", \"Group1\")) { x = \"x\"; y = \"y\" } +\n",
    "    geomRect(xmin = 6, xmax = 8, ymin = 0.7, ymax = 0.8, fill = \"green\", alpha = 0.2, \n",
    "             manualKey = layerKey(\"Green zone\", \"Group2\", alpha = 1)) +\n",
    "    geomLabel(label = \"Text\", x = 6, y = 0.2, fill = \"orange\", color = \"white\", size = 8, \n",
    "              manualKey = layerKey(\"Orange zone\", \"Group3\", color = \"black\"))\n",
    "\n",
    "\n",
    "p2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6639a40b-90f4-4203-aedb-e82eeee071b9",
   "metadata": {},
   "source": [
    "#### Append items to existing legend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ae4f6643-acda-4fb9-a0ec-7fc3a9bb0ca2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"XIRQWo\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"color\":\"x\",\n",
       "\"size\":\"v\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"v\":[\"c\",\"b\",\"c\",\"b\",\"a\",\"c\"],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"manual_key\":{\n",
       "\"shape\":21,\n",
       "\"label\":\"point\",\n",
       "\"group\":\"x\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}],\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"line\",\n",
       "\"group\":\"x\"\n",
       "},\n",
       "\"linetype\":4.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"XIRQWo\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// legend 'group' name = default title (variable name)\n",
    "\n",
    "val p3 = letsPlot() +\n",
    "    geomPoint(data = mapOf(\"x\" to x, \"y\" to y1, \"v\" to v),\n",
    "              manualKey = layerKey(\"point\", \"x\", shape = 21)\n",
    "              ){ x = \"x\"; y = \"y\"; color = asDiscrete(\"x\"); size = \"v\" } +\n",
    "    geomLine(data = mapOf(\"x\" to x, \"y\" to y2),\n",
    "             linetype = 4, color = \"blue\",\n",
    "             manualKey = layerKey(\"line\", \"x\")) { x = \"x\"; y = \"y\" }\n",
    "\n",
    "p3  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3e2233b3-6caf-401c-b499-dd72a3ad9eda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"MIXpFv\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"color\",\n",
       "\"discrete\":true,\n",
       "\"name\":\"My title\"\n",
       "},{\n",
       "\"aesthetic\":\"size\",\n",
       "\"name\":\"My title\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"color\":\"x\",\n",
       "\"size\":\"v\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"v\":[\"c\",\"b\",\"c\",\"b\",\"a\",\"c\"],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"manual_key\":{\n",
       "\"shape\":21,\n",
       "\"label\":\"point\",\n",
       "\"group\":\"x\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}],\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"line\",\n",
       "\"group\":\"x\"\n",
       "},\n",
       "\"linetype\":4.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"MIXpFv\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Change titles for scales - will be combined, but custom legend still has 'x' name\n",
    "\n",
    "p3 + scaleColorDiscrete(name = \"My title\") + scaleSize(name = \"My title\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e1746533-8ad4-421d-a851-cc98f5493434",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"LxjYVU\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"x\":{\n",
       "\"title\":\"My title\"\n",
       "}\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"color\",\n",
       "\"discrete\":true,\n",
       "\"name\":\"My title\"\n",
       "},{\n",
       "\"aesthetic\":\"size\",\n",
       "\"name\":\"My title\"\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"color\":\"x\",\n",
       "\"size\":\"v\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"v\":[\"c\",\"b\",\"c\",\"b\",\"a\",\"c\"],\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"manual_key\":{\n",
       "\"shape\":21,\n",
       "\"label\":\"point\",\n",
       "\"group\":\"x\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}],\n",
       "\"mapping_annotations\":[{\n",
       "\"aes\":\"color\",\n",
       "\"annotation\":\"as_discrete\",\n",
       "\"parameters\":{\n",
       "\"label\":\"x\"\n",
       "}\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"line\",\n",
       "\"group\":\"x\"\n",
       "},\n",
       "\"linetype\":4.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"LxjYVU\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Change title for 'x'\n",
    "\n",
    "p3 + scaleColorDiscrete(name = \"My title\") + scaleSize(name = \"My title\") + labsAlt(\"x\" to \"My title\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a8af6b6-0ab8-4d3c-b5af-bf87a86771f1",
   "metadata": {},
   "source": [
    "#### Title using `guides()` and `guidesAlt()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "befd4175-f24d-4ea7-82b9-d00663509c73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"NDO1Po\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"manual\":{\n",
       "\"name\":\"legend\",\n",
       "\"title\":\"Color zones\",\n",
       "\"nrow\":2.0,\n",
       "\"override_aes\":{\n",
       "}\n",
       "}\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Red zone\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"Blue zone\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Green zone\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"label\":\"Orange zone\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"NDO1Po\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// default legend group - use key 'manual'\n",
    "\n",
    "p1 + guides(manual = guideLegend(title = \"Color zones\", nrow = 2)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d60d7ffd-ada4-4135-8c91-9ec30e8020b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"HtLJBP\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"Group1\":{\n",
       "\"name\":\"legend\",\n",
       "\"title\":\"Zone 1\",\n",
       "\"ncol\":2.0,\n",
       "\"override_aes\":{\n",
       "}\n",
       "},\n",
       "\"Group2\":{\n",
       "\"name\":\"legend\",\n",
       "\"title\":\"Zone 2\",\n",
       "\"override_aes\":{\n",
       "}\n",
       "},\n",
       "\"Group3\":{\n",
       "\"name\":\"legend\",\n",
       "\"title\":\"Zone 3\",\n",
       "\"override_aes\":{\n",
       "}\n",
       "}\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Red zone\",\n",
       "\"group\":\"Group1\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"Blue zone\",\n",
       "\"group\":\"Group1\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Green zone\",\n",
       "\"group\":\"Group2\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"label\":\"Orange zone\",\n",
       "\"group\":\"Group3\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"HtLJBP\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// named legend groups\n",
    "\n",
    "p2 + guidesAlt(\"Group1\" to guideLegend(title = \"Zone 1\", ncol = 2), \n",
    "               \"Group2\" to guideLegend(title = \"Zone 2\"), \n",
    "               \"Group3\" to guideLegend(title = \"Zone 3\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1a34b63-17bc-4f04-b633-f16382ec0b45",
   "metadata": {},
   "source": [
    "#### Title using `labs()` and `labsAlt()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "613509d5-6c6f-4782-afa3-3ada6e8b81c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"0MdyRq\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"manual\":{\n",
       "\"title\":\"Zones\",\n",
       "\"name\":\"legend\",\n",
       "\"ncol\":2.0,\n",
       "\"override_aes\":{\n",
       "}\n",
       "}\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Red zone\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"Blue zone\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Green zone\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"label\":\"Orange zone\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"0MdyRq\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// default legend group in `labs()` - use key 'manual'\n",
    "\n",
    "p1 + labs(manual = \"Zones\") + guides(manual = guideLegend(ncol = 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "409525b0-2825-4b04-8999-5a188965c003",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"l1cDpd\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "},\n",
       "\"guides\":{\n",
       "\"Group1\":{\n",
       "\"title\":\"Zone 1\"\n",
       "},\n",
       "\"Group2\":{\n",
       "\"title\":\"\"\n",
       "}\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.7275636800328681,0.6832234717598454,0.30871945533265976,0.27707849007413665,0.6655489517945736,0.9033722646721782]\n",
       "},\n",
       "\"color\":\"red\",\n",
       "\"shape\":21.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Red zone\",\n",
       "\"group\":\"Group1\"\n",
       "},\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"data\":{\n",
       "\"x\":[0.0,1.0,2.0,3.0,4.0,5.0],\n",
       "\"y\":[0.36878291341130565,0.2757480694417024,0.46365357580915334,0.7829017787900358,0.9193277828687169,0.43649097442328655]\n",
       "},\n",
       "\"color\":\"blue\",\n",
       "\"manual_key\":{\n",
       "\"label\":\"Blue zone\",\n",
       "\"group\":\"Group1\"\n",
       "},\n",
       "\"linetype\":2.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data_meta\":{\n",
       "\"series_annotations\":[{\n",
       "\"type\":\"int\",\n",
       "\"column\":\"x\"\n",
       "},{\n",
       "\"type\":\"float\",\n",
       "\"column\":\"y\"\n",
       "}]\n",
       "}\n",
       "},{\n",
       "\"ymin\":0.7,\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"xmin\":6.0,\n",
       "\"ymax\":0.8,\n",
       "\"xmax\":8.0,\n",
       "\"manual_key\":{\n",
       "\"label\":\"Green zone\",\n",
       "\"group\":\"Group2\",\n",
       "\"alpha\":1\n",
       "},\n",
       "\"alpha\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"rect\",\n",
       "\"fill\":\"green\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"white\",\n",
       "\"size\":8.0,\n",
       "\"manual_key\":{\n",
       "\"color\":\"black\",\n",
       "\"label\":\"Orange zone\",\n",
       "\"group\":\"Group3\"\n",
       "},\n",
       "\"x\":6.0,\n",
       "\"y\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"label\":\"Text\",\n",
       "\"geom\":\"label\",\n",
       "\"fill\":\"orange\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"l1cDpd\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 + labsAlt(\"Group1\" to \"Zone 1\", \"Group2\" to \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e02918c1-6915-40d4-bd6a-5b4045b0dfa4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
